#--coding:utf-8--
# @Time    : 2022/5/09 02:05
# @Author  : hakold
# @File    : item_model.py
# @PROJECT : chatroom_game

import json
import pymongo
from model import player_model

global item_config
item_config = json.load(open("config/item_config.json", encoding='utf-8'))

def mongo_connect():
    #myclient = pymongo.MongoClient('mongodb+srv://hakold:52Beebee@cluster0.gy1rb.mongodb.net/xiuxian?retryWrites=true&w=majority/')
    myclient = pymongo.MongoClient('mongodb://127.0.0.1:27017/')
    #dblist = myclient.list_database_names()
    # dblist = myclient.database_names() 
    mydb = myclient["qt_game"] #创建数据库
    #collist = mydb.list_collection_names()
    mycol = mydb["player"] #集合
    return mycol

def bag_add_item(account, item_id, item_num):
    # 背包增加道具方法
    # 背包为字典类型
    # 参数：
    # item_id： 道具id
    # item_num: 增加道具数量
    # key 为 道具id ， value 为道具数量
    # 增加时先判断是否存在key
    # 如果存在就value+1，不存在则新增一个key
    mycol = mongo_connect()
    # 获取玩家数据
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0] # 玩家数据
    del player_data["_id"]

    if  str(item_id) in player_data['bag']:
        player_data['bag'][str(item_id)] += item_num
    else :
        new_item = {str(item_id): item_num }
        player_data['bag'].update(new_item)
    
    newvalues = { "$set": { "bag": player_data['bag'] } }
    mycol.update_one({ "account" : account }, newvalues)

def bag_use_item(account, item_id, item_num):
    # 背包增加道具方法
    # 背包为字典类型
    # 参数：
    # item_id： 道具id
    # item_num: 增加道具数量
    # key 为 道具id ， value 为道具数量
    # 增加时先判断是否存在key
    # 如果存在就value+1，不存在则新增一个key
    mycol = mongo_connect()
    # 获取玩家数据
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0] # 玩家数据
    del player_data["_id"]

    if str(item_id) in player_data['bag']:
        if player_data['bag'][str(item_id)] > 1:
            # 道具数量大于1
            player_data['bag'][str(item_id)] -= item_num
        elif player_data['bag'][str(item_id)] == 1:
            # 道具数量等于1
            del player_data['bag'][str(item_id)]
    
    newvalues = { "$set": { "bag": player_data['bag'] } }
    mycol.update_one({ "account" : account }, newvalues)


def player_bag_get(data_dict):
    # 玩家背包查询方法
    # 背包为字典类型
    data = {}
    account = data_dict['account']
    mycol = mongo_connect()
    # 获取玩家数据
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0] # 玩家数据
    del player_data["_id"]
    code = "200"
    data["account"] = player_data["account"]
    data["type"] = "bag_get"
    data["code"] = code
    data["data"] = player_data
    data = json.dumps(data)
    return data

def player_bag_item_use(self, data_dict):
    # 玩家背包道具使用
    data = {}
    account = data_dict['account']
    item_id = data_dict['item_id']
    print(str(account) + "使用道具" + str(item_id))
    global item_config
    mycol = mongo_connect()
    # 获取玩家数据
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0] # 玩家数据
    del player_data["_id"]
    for item_index in item_config:
        if item_config[item_index]['id'] == item_id:
            item_name = item_config[item_index]['name']
            item_effect = item_config[item_index]['effect'].split("|")
            effect_type = item_effect[0] # 道具使用获得的属性 (str)
            effect_value = item_effect[1] # 道具使用获得的值 (str)
            # 玩家消耗道具
            bag_use_item(account, item_id, 1)
            # 玩家获得效果
            player_model.player_attribute_change(self, account, effect_type, int(effect_value))
            msg = ("使用道具"+ "【" +item_name + "】""成功")
            data["message"] = msg
            code = "200"
        else:
            code = "500"
    data["account"] = player_data["account"]
    data["type"] = "item_use"
    data["code"] = code
    data["data"] = player_data
    data = json.dumps(data)
    return data